(require (planet schematics/schemeunit:3))

(define (square x)
  (* x x))

(define (even? n)
  (= (remainder n 2) 0))

(define (fast-expt b n)
  (fast-expt-iter 1 b n))

(define (fast-expt-iter a b n)
  (cond ((= n 0) a)
        ((even? n) (fast-expt-iter a (square b) (/ n 2)))
        (else (fast-expt-iter (* b a) (square b) (/ (- n 1) 2)))))

(check-equal? (fast-expt 3 7) (expt 3 7))
(check-equal? (fast-expt 2 255) (expt 2 255))
